package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.ChannelInfo;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by E540 on 2015/3/13.
 */
public class ChannelInfoAdapter extends RecyclerView.Adapter<ChannelInfoAdapter.ChannelInfoViewHolder> {
    private Context context;
    private List<ChannelInfo> data;
    private OnClickCallBack listener;

    public ChannelInfoAdapter(Context context, List<ChannelInfo> data) {
        this.context = context;
        this.data = data;
    }

    public void setOnClickListener(OnClickCallBack listener) {
        this.listener = listener;
    }

    @Override
    public ChannelInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_channel_info_list, parent, false);
        return new ChannelInfoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChannelInfoViewHolder holder, int position) {
        holder.listener = this.listener;
        ChannelInfo channelInfo = data.get(position);
        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        holder.name.setText(channelInfo.getName());
        holder.score.setText(channelInfo.getScore() + "分");
        if ("1".equals(channelInfo.getIsOver())) {
            //已完结
            holder.total.setText("全" + channelInfo.getTotalNum() + "集");
        } else {
            holder.total.setText("更新至" + channelInfo.getCurNum() + "集");
        }
        bitmapUtils.configDefaultLoadingImage(R.drawable.item_default);
        bitmapUtils.display(holder.pic, channelInfo.getCover());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ChannelInfoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @ViewInject(R.id.item_channel_info_pic)
        public ImageView pic;
        @ViewInject(R.id.item_channel_info_name)
        public TextView name;
        @ViewInject(R.id.item_channel_info_total)
        public TextView total;
        @ViewInject(R.id.item_channel_info_score)
        public TextView score;
        public OnClickCallBack listener;

        public ChannelInfoViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(v, getPosition());
            }
        }
    }
}
